package com.hisense.ovcloud.asop.config;

import com.mybatisflex.core.audit.AuditManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfigMybatisLog {
    private static final Logger logger = LoggerFactory
            .getLogger("mybatis-flex-sql");

    @Value("${spring.flexLogEnable}")
    private boolean mybatisFlexLogEnable;

    @Bean
    public void fetchDomainsInYaml() {
        if(mybatisFlexLogEnable){
            //开启审计功能
            AuditManager.setAuditEnable(true);

            //设置 SQL 审计收集器
            AuditManager.setMessageCollector(auditMessage ->
                    logger.info("{},{}ms", auditMessage.getFullSql()
                            , auditMessage.getElapsedTime())
            );
        }
    }
}
